<style>
  .header-font {
    font-size: 15px;
    font-weight: bold;
  }
  .line {
    margin: 10px 0;
    width: 100%;
    border: 0.5px solid rgb(218, 218, 218);
  }
  .content-form {
    margin-bottom: -10px;
  }
  .content-paging {
    float:right;
    margin-right: 35px;
  }
  .content-list {
    clear: both;
    margin-top: 20px;
  }
  .add-btn {
    margin-right: 10px;
  }
  .content-radio {
    margin-top: -30px;
    margin-left: 150px;
  }
</style>
<template>
  <div>
    <div class='header-font'>
      <Icon type="md-apps" />
      <span>视频列表</span>
    </div>
    <div class="line"></div>
    <content-form
      class="content-form"
      @on-success-valid="handleSubmit">
    </content-form>
    <Button
      icon="md-add"
      type="primary"
      class="add-btn"
      @click="handleClick">
      添加内容
    </Button>
    <video-modal v-if="modalState" :modalState="modalState" @video-valid="handleEdit"></video-modal>
    <confirm-modal
      @confirm-valid="handleConfirm">
    </confirm-modal>
    <delete-modal
      @delete-valid="confirmDelete">
    </delete-modal>
    <content-list class="content-list" :list="historyData" @view-content="viewVideo" @delete-content="deleteVideo" @edit-content="handleClick"></content-list>
    <table-page
      class="content-paging"
      :dataCount="dataCount"
      @change-page="changePage"
      @change-page-size="changePageSize"
      :pageSize="pageSize">
    </table-page>
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
import ContentForm from '_c/content-form'
import TablePage from '_c/table-page'
import ContentList from '_c/content-list'
import ContentRadio from '_c/content-radio'
import ConfirmModal from '_c/confirm-modal'
import DeleteModal from '_c/delete-modal'
import VideoModal from '_c/video-modal'
export default {
  components: {
    ContentForm,
    TablePage,
    ContentList,
    ContentRadio,
    ConfirmModal,
    DeleteModal,
    VideoModal
  },
  data () {
    return {
      page: 1,
      title: '内容管理图文列表',
      value: '视频',
      rows: 20,
      confirmData: {},
      img: '',
      postData: {},
      playload: null
    }
  },
  computed: {
    ...mapState({
      videoList: state => state.video.videoList,
      dataCount: state => state.video.videoDataCount,
      historyData: state => state.video.videoHistoryData,
      pageSize: state => state.video.videoPageSize,
      modalState: state => state.modal.modalState
    })
  },
  methods: {
    ...mapActions([
      'getVideoList',
      'getVideoListCurrentRow',
      'editVideoListData',
      'updateModalState',
      'updateConfirmModalState',
      'updateDeleteModalState',
      'changeVideoListPage',
      'changeVideoListPageSize'
    ]),
    handleSubmit (playload) {
      this.postData = {
        ctime: playload.ctime,
        title: playload.title,
        type: 2
      }
      this.playload = this.postData
      this.getVideoList(this.postData)
    },
    handleClick (item) {
      this.img = item.img ? item.img : ''
      let newRow = {}
      this.id = item.id
      if (item.id) {
        newRow = Object.assign({}, item)
      }
      this.getVideoListCurrentRow(newRow)
      this.updateModalState(true)
    },
    viewVideo () {

    },
    handleEdit (playload) {
      let upImg = playload.img ? playload.img : this.img
      if (upImg === '') {
        this.$Message.warning('请上传页面图片！')
        return false
      }
      if (!playload.content) {
        this.$Message.warning('请编辑页面内容！')
        return false
      }
      this.confirmData = playload
      this.updateModalState(false)
      this.updateConfirmModalState(true)
    },
    changePage (index) {
      this.page = index
      this.changeVideoListPage(index)
      this.getVideoList({
        page: index,
        type: 2,
        rows: this.rows,
        ...this.playload
      })
    },
    changePageSize (value) {
      this.rows = value
      this.changeVideoListPageSize(value)
      this.changePage(this.page)
      this.getVideoList({
        type: 2,
        page: index,
        rows: value,
        ...this.playload
      })
    },
    changeContent (value) {
      if (value === 'H5') {
        this.$router.push('/h5-content')
      } else if (value === '图文') {
        this.$router.push('/graphic')
      } else if (value === '视频') {
        this.$router.push('/video')
      }
    },
    deleteVideo (index) {
      this.index = index
      this.updateDeleteModalState(true)
    },
    async handleConfirm () {
      const resp = await this.editVideoListData(this.confirmData)
      if (resp.data.state === '000') {
        this.$Message.success('保存成功')
        this.timeout = setTimeout(() => {
          this.getVideoList({
            page: this.page,
            type: 2,
            rows: this.rows,
            ...this.playload
          })
        }, 100)
      } else {
        this.$Message.error(resp.data.msg)
      }
      this.updateConfirmModalState(false)
    },
    confirmDelete () {
      this.updateDeleteModalState(false)
      this.videoList.splice(this.index, 1)
      this.changePage(this.page)
    }
  },
  created () {
    this.getVideoList({
      type: 2
    })
  }
}
</script>
